<?php

namespace App\Admin\Controllers;

use App\Models\Element;
use App\Models\Veidoo;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;


class ElementController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('指标体系')
            ->description('要素设置')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('指标体系')
            ->description('要素详情信息')
            ->body($this->detail($id));

    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
        ->header('指标体系')
        ->description('编辑要素信息')
        ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('指标体系')
            ->description('新建要素信息')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Element);

        $grid->id('Id')->sortable()->style('width:80px');
        $grid->column('veidoo.name','所属维度')->style('width:100px');
        $grid->name('要素名称')->style('width:120px');
        $grid->content('要素内容');
        $grid->score('最高分')->style('width:60px');

        $grid->paginate(10);
//        $grid->disableFilter();
        $grid->actions(function ($actions) {
            $actions->disableView();
        });

        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $veioos = Veidoo::get();
            $veioo_arr = [];

            foreach ($veioos as $veioo) {
                $veioo_arr[$veioo->id] = $veioo->name;
            }
            $filter->equal('veidoo_id', '所属维度')->select($veioo_arr);


        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Element::findOrFail($id));


        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Element());

        $veioos = Veidoo::get();
        $veioo_arr = [];

        foreach ($veioos as $veioo) {
            $veioo_arr[$veioo->id] = $veioo->name;
        }

        $form->select('veidoo_id', trans('维度'))->options($veioo_arr)->setWidth(3, 2);

        $form->text('name','要素名称')->rules('required', [
            'required' => '要素名称不能为空'
        ])->setWidth(4, 2);
        $form->textarea('content','要素内容')->rules('required', [
            'required' => '要素内容不能为空'
        ])->setWidth(4, 2)->rows(2);
        $form->number('score','最高得分')->min(1)->max(5);

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });

        return $form;
    }
}
